﻿using Speed.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace XX
{


    [Serializable]
    public partial class DataClassSales : DataClass
    {

        //    public Record CreateInstance()
        //    {
        //        return new TesteGen.Poco.Sales();
        //    }

        private void readRecord(DbDataReader dr, ref TesteGen.Poco.Sales value)
        {
            //value.RecordStatus = RecordStatus.Existing;
            value.TestPerfID = dr.GetInt32(0);
            value.CarrierTrackingNumber = dr.GetString(1);
            value.OrderQty = dr.GetInt32(2);
            value.ProductID = dr.GetInt32(3);
            value.SpecialOfferID = dr.GetInt32(4);
            value.UnitPrice = dr.GetDecimal(5);
            value.UnitPriceDiscount = dr.GetDecimal(6);
            value.LineTotal = dr.GetDecimal(7);
            value.LongText = dr.GetString(8);
            value.rowguid = dr.GetGuid(9);
            value.ModifiedDate = dr.GetDateTime(10);

        }

        private void readRecordSql(DbDataReader dr, List<string> names, ref TesteGen.Poco.Sales value)
        {
            //value.RecordStatus = RecordStatus.Existing;
            if (names.Contains("TestPerfID")) value.TestPerfID = dr.GetInt32(1);
            if (names.Contains("CarrierTrackingNumber")) value.CarrierTrackingNumber = dr.GetString(2);
            if (names.Contains("OrderQty")) value.OrderQty = dr.GetInt32(3);
            if (names.Contains("ProductID")) value.ProductID = dr.GetInt32(4);
            if (names.Contains("SpecialOfferID")) value.SpecialOfferID = dr.GetInt32(5);
            if (names.Contains("UnitPrice")) value.UnitPrice = dr.GetDecimal(6);
            if (names.Contains("UnitPriceDiscount")) value.UnitPriceDiscount = dr.GetDecimal(7);
            if (names.Contains("LineTotal")) value.LineTotal = dr.GetDecimal(8);
            if (names.Contains("LongText")) value.LongText = dr.GetString(9);
            if (names.Contains("rowguid")) value.rowguid = dr.GetGuid(10);
            if (names.Contains("ModifiedDate")) value.ModifiedDate = dr.GetDateTime(11);

        }

        private List<TesteGen.Poco.Sales> readReader(Database db, string sql, bool concurrency)
        {
            using (DbDataReader dr = db.ExecuteReader(sql))
            {
                List<TesteGen.Poco.Sales> list = new List<TesteGen.Poco.Sales>();
                while (dr.Read())
                {
                    TesteGen.Poco.Sales value = new TesteGen.Poco.Sales();
                    readRecord(dr, ref value);
                    //if (concurrency)
                    //    value.RecordOriginal = value.CloneT();
                    list.Add(value);
                }
                return list;
            }
        }

        private List<TesteGen.Poco.Sales> readReader(Database db, string sql, bool concurrency, params Parameter[] parameters)
        {
            using (DbDataReader dr = db.ExecuteReader(sql, parameters))
            {
                List<TesteGen.Poco.Sales> list = new List<TesteGen.Poco.Sales>();
                while (dr.Read())
                {
                    TesteGen.Poco.Sales value = new TesteGen.Poco.Sales();
                    readRecord(dr, ref value);
                    //if (concurrency)
                    //    value.RecordOriginal = value.CloneT();
                    list.Add(value);
                }
                return list;
            }
        }

        private TesteGen.Poco.Sales readReaderSingle(Database db, string sql, bool concurrency)
        {
            TesteGen.Poco.Sales value = null;
            using (DbDataReader dr = db.ExecuteReader(sql))
            {
                List<TesteGen.Poco.Sales> list = new List<TesteGen.Poco.Sales>();
                if (dr.Read())
                {
                    value = new TesteGen.Poco.Sales();
                    readRecord(dr, ref value);
                    //if (concurrency)
                    //    value.RecordOriginal = value.CloneT();
                }
                return value;
            }
        }

        private TesteGen.Poco.Sales readReaderSingle(Database db, string sql, Parameter[] parameters)
        {
            TesteGen.Poco.Sales value = null;
            using (DbDataReader dr = db.ExecuteReader(sql, parameters))
            {
                List<TesteGen.Poco.Sales> list = new List<TesteGen.Poco.Sales>();
                if (dr.Read())
                {
                    value = new TesteGen.Poco.Sales();
                    readRecord(dr, ref value);
                }
                return value;
            }
        }

        private List<TesteGen.Poco.Sales> readReaderSql(Database db, string sql, bool concurrency)
        {
            using (DbDataReader dr = db.ExecuteReader(sql))
            {
                List<TesteGen.Poco.Sales> list = new List<TesteGen.Poco.Sales>();
                // pega os nomes das colunas continas no sql
                List<string> names = new List<string>();
                for (int i = 0; i < dr.FieldCount; i++)
                    names.Add(dr.GetName(i));
                while (dr.Read())
                {
                    TesteGen.Poco.Sales value = new TesteGen.Poco.Sales();
                    readRecordSql(dr, names, ref value);
                    //if (concurrency)
                    //    value.RecordOriginal = value.CloneT();
                    list.Add(value);
                }
                return list;
            }
        }

        private List<TesteGen.Poco.Sales> select(Database db, bool concurrency)
        {
            string sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales";
            return readReader(db, sql, concurrency);
        }

        private List<TesteGen.Poco.Sales> select(Database db, string where, bool concurrency)
        {
            string sql;
            if (string.IsNullOrWhiteSpace(where))
                sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales";
            else
                sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales where " + where;
            return readReader(db, sql, concurrency);
        }

        private List<TesteGen.Poco.Sales> select(Database db, string where, params Parameter[] parameters)
        {
            string sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales where " + where;
            return readReader(db, sql, false, parameters);
        }

        private List<TesteGen.Poco.Sales> select(Database db, string where, bool concurrency, params Parameter[] parameters)
        {
            string sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales where " + where;
            return readReader(db, sql, concurrency, parameters);
        }

        private List<TesteGen.Poco.Sales> selectTop(Database db, int top, bool concurrency)
        {
            // string sql = string.Format("db.Provider.SetTop(\"select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales\", {0})", top);
            string sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales";
            sql = db.Provider.SetTop(sql, top);
            return readReader(db, sql, concurrency);
        }

        private List<TesteGen.Poco.Sales> selectTop(Database db, int top, string where, bool concurrency)
        {
            string sql;
            if (string.IsNullOrWhiteSpace(where))
                sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales";
            else
                sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales where " + where;
            sql = db.Provider.SetTop(sql, top);
            return readReader(db, sql, concurrency);
        }

        private long count(Database db, string where)
        {
            string sql;
            if (string.IsNullOrWhiteSpace(where))
                sql = "select count(*) from Sales";
            else
                sql = "select count(*) from Sales where " + where;
            return db.ExecuteInt64(sql);
        }

        private long max(Database db, string columnName, string where)
        {
            string sql = string.Format("select count(*) from Sales", columnName);
            if (!string.IsNullOrWhiteSpace(where))
                sql += " where " + where;
            return db.ExecuteInt64(sql);
        }

        private List<TesteGen.Poco.Sales> selectColumns(Database db, bool concurrency, params string[] columns)
        {
            string sql = "select " + Concat(", ", columns) + " from Sales";
            return readReaderSql(db, sql, concurrency);
        }

        private List<TesteGen.Poco.Sales> selectColumns(Database db, string where, bool concurrency, params string[] columns)
        {
            string sql;
            if (string.IsNullOrWhiteSpace(where))
                sql = "select " + Concat(", ", columns) + " from Sales ";
            else
                sql = "select " + Concat(", ", columns) + " from Sales where " + where;
            return readReaderSql(db, sql, concurrency);
        }

        private TesteGen.Poco.Sales selectSingle(Database db, bool concurrency)
        {
            string sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales";
            return readReaderSingle(db, sql, concurrency);
        }

        private TesteGen.Poco.Sales selectSingle(Database db, string where, bool concurrency)
        {
            string sql;
            if (string.IsNullOrWhiteSpace(where))
                sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales";
            else
                sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales where " + where;
            return readReaderSingle(db, sql, concurrency);
        }

        private object selectByPk(Database db, object instance, bool concurrency)
        {
            TesteGen.Poco.Sales value = (TesteGen.Poco.Sales)instance;
            TesteGen.Poco.Sales value2 = new TesteGen.Poco.Sales();
            string sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales where TestPerfID = @P_TestPerfID";
            using (DbCommand cmd = db.NewCommand(sql))
            {
                db.AddWithValue(cmd, "@P_TestPerfID", GetValue(value.TestPerfID));

                using (DbDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        readRecord(dr, ref value2);
                        //if (concurrency)
                        //    value2.RecordOriginal = value2.CloneT();
                        return value2;
                    }
                    else
                        return null;
                }
            }
        }

        private int insert(Database db, object instance)
        {

            TesteGen.Poco.Sales value = (TesteGen.Poco.Sales)instance;
            string sql = "insert into Sales (CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate) values (@P_CarrierTrackingNumber, @P_OrderQty, @P_ProductID, @P_SpecialOfferID, @P_UnitPrice, @P_UnitPriceDiscount, @P_LineTotal, @P_LongText, @P_rowguid, @P_ModifiedDate)";
            using (DbCommand cmd = db.NewCommand(sql))
            {
                db.AddWithValue(cmd, "@P_CarrierTrackingNumber", "String", GetValue(value.CarrierTrackingNumber));
                db.AddWithValue(cmd, "@P_OrderQty", "Int32", GetValue(value.OrderQty));
                db.AddWithValue(cmd, "@P_ProductID", "Int32", GetValue(value.ProductID));
                db.AddWithValue(cmd, "@P_SpecialOfferID", "Int32", GetValue(value.SpecialOfferID));
                db.AddWithValue(cmd, "@P_UnitPrice", "Decimal", GetValue(value.UnitPrice));
                db.AddWithValue(cmd, "@P_UnitPriceDiscount", "Decimal", GetValue(value.UnitPriceDiscount));
                db.AddWithValue(cmd, "@P_LineTotal", "Decimal", GetValue(value.LineTotal));
                db.AddWithValue(cmd, "@P_LongText", "String", GetValue(value.LongText));
                db.AddWithValue(cmd, "@P_rowguid", "Guid", GetValue(value.rowguid));
                db.AddWithValue(cmd, "@P_ModifiedDate", "DateTime", GetValue(value.ModifiedDate));

                //value.RecordStatus = RecordStatus.Existing;
                return db.ExecuteNonQuery(cmd);
            }

        }

        private int insertRequery(Database db, object instance, bool concurrency)
        {

            TesteGen.Poco.Sales value = (TesteGen.Poco.Sales)instance;
            string sql;
            sql = "insert into Sales (CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate) values (@P_CarrierTrackingNumber, @P_OrderQty, @P_ProductID, @P_SpecialOfferID, @P_UnitPrice, @P_UnitPriceDiscount, @P_LineTotal, @P_LongText, @P_rowguid, @P_ModifiedDate);";
            sql += "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales where TestPerfID = SCOPE_IDENTITY();";
            using (DbCommand cmd = db.NewCommand(sql))
            {
                db.AddWithValue(cmd, "@P_CarrierTrackingNumber", "String", GetValue(value.CarrierTrackingNumber));
                db.AddWithValue(cmd, "@P_OrderQty", "Int32", GetValue(value.OrderQty));
                db.AddWithValue(cmd, "@P_ProductID", "Int32", GetValue(value.ProductID));
                db.AddWithValue(cmd, "@P_SpecialOfferID", "Int32", GetValue(value.SpecialOfferID));
                db.AddWithValue(cmd, "@P_UnitPrice", "Decimal", GetValue(value.UnitPrice));
                db.AddWithValue(cmd, "@P_UnitPriceDiscount", "Decimal", GetValue(value.UnitPriceDiscount));
                db.AddWithValue(cmd, "@P_LineTotal", "Decimal", GetValue(value.LineTotal));
                db.AddWithValue(cmd, "@P_LongText", "String", GetValue(value.LongText));
                db.AddWithValue(cmd, "@P_rowguid", "Guid", GetValue(value.rowguid));
                db.AddWithValue(cmd, "@P_ModifiedDate", "DateTime", GetValue(value.ModifiedDate));

                using (DbDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        readRecord(dr, ref value);
                        //value.RecordStatus = RecordStatus.Existing;
                        //if (concurrency)
                        //    value.RecordOriginal = value.CloneT();
                        return 1;
                    }
                    return 0;
                }
            };
        }

        public override int Update(Database db, object instance)
        {
            return update(db, instance);
        }

        public override int Update(Database db, object instance, EnumSaveMode saveMode, bool concurrency = false)
        {
            if (saveMode == EnumSaveMode.None)
                return update(db, instance);
            else
                return updateRequery(db, instance, concurrency);
        }

        private int update(Database db, object instance)
        {
            TesteGen.Poco.Sales value = (TesteGen.Poco.Sales)instance;
            //if (value.RecordOriginal as TesteGen.Poco.Sales != null) CheckConcurrency(db, value);
            string sql = "update Sales set CarrierTrackingNumber = @P_CarrierTrackingNumber, OrderQty = @P_OrderQty, ProductID = @P_ProductID, SpecialOfferID = @P_SpecialOfferID, UnitPrice = @P_UnitPrice, UnitPriceDiscount = @P_UnitPriceDiscount, LineTotal = @P_LineTotal, LongText = @P_LongText, rowguid = @P_rowguid, ModifiedDate = @P_ModifiedDate where TestPerfID = @P_OldTestPerfID";
            using (DbCommand cmd = db.NewCommand(sql))
            {
                db.AddWithValue(cmd, "@P_TestPerfID", "Int32", GetValue(value.TestPerfID));
                db.AddWithValue(cmd, "@P_CarrierTrackingNumber", "String", GetValue(value.CarrierTrackingNumber));
                db.AddWithValue(cmd, "@P_OrderQty", "Int32", GetValue(value.OrderQty));
                db.AddWithValue(cmd, "@P_ProductID", "Int32", GetValue(value.ProductID));
                db.AddWithValue(cmd, "@P_SpecialOfferID", "Int32", GetValue(value.SpecialOfferID));
                db.AddWithValue(cmd, "@P_UnitPrice", "Decimal", GetValue(value.UnitPrice));
                db.AddWithValue(cmd, "@P_UnitPriceDiscount", "Decimal", GetValue(value.UnitPriceDiscount));
                db.AddWithValue(cmd, "@P_LineTotal", "Decimal", GetValue(value.LineTotal));
                db.AddWithValue(cmd, "@P_LongText", "String", GetValue(value.LongText));
                db.AddWithValue(cmd, "@P_rowguid", "Guid", GetValue(value.rowguid));
                db.AddWithValue(cmd, "@P_ModifiedDate", "DateTime", GetValue(value.ModifiedDate));

                //TesteGen.Poco.Sales old = (value.RecordOriginal as TesteGen.Poco.Sales) != null ? (TesteGen.Poco.Sales)value.RecordOriginal : value;
                TesteGen.Poco.Sales old = value;
                db.AddWithValue(cmd, "@P_OldTestPerfID", GetValue(old.TestPerfID));

                return db.ExecuteNonQuery(cmd);
            }
        }

        private int updateRequery(Database db, object instance, bool concurrency = false)
        {

            TesteGen.Poco.Sales value = (TesteGen.Poco.Sales)instance;
            //if (value.RecordOriginal as TesteGen.Poco.Sales != null) CheckConcurrency(db, value);
            string sql;
            sql = "update Sales set CarrierTrackingNumber = @P_CarrierTrackingNumber, OrderQty = @P_OrderQty, ProductID = @P_ProductID, SpecialOfferID = @P_SpecialOfferID, UnitPrice = @P_UnitPrice, UnitPriceDiscount = @P_UnitPriceDiscount, LineTotal = @P_LineTotal, LongText = @P_LongText, rowguid = @P_rowguid, ModifiedDate = @P_ModifiedDate where TestPerfID = @P_OldTestPerfID;";
            sql += "\r\n" + "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales where TestPerfID = @P_TestPerfID;";
            using (DbCommand cmd = db.NewCommand(sql))
            {
                db.AddWithValue(cmd, "@P_TestPerfID", "Int32", GetValue(value.TestPerfID));
                db.AddWithValue(cmd, "@P_CarrierTrackingNumber", "String", GetValue(value.CarrierTrackingNumber));
                db.AddWithValue(cmd, "@P_OrderQty", "Int32", GetValue(value.OrderQty));
                db.AddWithValue(cmd, "@P_ProductID", "Int32", GetValue(value.ProductID));
                db.AddWithValue(cmd, "@P_SpecialOfferID", "Int32", GetValue(value.SpecialOfferID));
                db.AddWithValue(cmd, "@P_UnitPrice", "Decimal", GetValue(value.UnitPrice));
                db.AddWithValue(cmd, "@P_UnitPriceDiscount", "Decimal", GetValue(value.UnitPriceDiscount));
                db.AddWithValue(cmd, "@P_LineTotal", "Decimal", GetValue(value.LineTotal));
                db.AddWithValue(cmd, "@P_LongText", "String", GetValue(value.LongText));
                db.AddWithValue(cmd, "@P_rowguid", "Guid", GetValue(value.rowguid));
                db.AddWithValue(cmd, "@P_ModifiedDate", "DateTime", GetValue(value.ModifiedDate));

                //TesteGen.Poco.Sales old = value.RecordOriginal as TesteGen.Poco.Sales != null ? (TesteGen.Poco.Sales)value.RecordOriginal : value;
                TesteGen.Poco.Sales old = value;
                db.AddWithValue(cmd, "@P_OldTestPerfID", GetValue(old.TestPerfID));

                using (DbDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        readRecord(dr, ref value);
                        //value.RecordStatus = RecordStatus.Existing;
                        //if (concurrency)
                        //    value.RecordOriginal = value.CloneT();
                        return 1;
                    }
                    return 0;
                }
            }

        }

        public override int Truncate(Database db)
        {
            string sql = "truncate table Sales";
            using (DbCommand cmd = db.NewCommand(sql))
                return db.ExecuteNonQuery(cmd);
        }

        public override int Delete(Database db)
        {
            string sql = "delete from Sales";
            using (DbCommand cmd = db.NewCommand(sql))
                return db.ExecuteNonQuery(cmd);
        }

        public override int Delete(Database db, object instance)
        {
            TesteGen.Poco.Sales value = (TesteGen.Poco.Sales)instance;
            string sql = "delete from Sales where TestPerfID = @P_OldTestPerfID";
            using (DbCommand cmd = db.NewCommand(sql))
            {
                //TesteGen.Poco.Sales old = (value.RecordOriginal as TesteGen.Poco.Sales) != null ? (TesteGen.Poco.Sales)value.RecordOriginal : value;
                TesteGen.Poco.Sales old = value;
                db.AddWithValue(cmd, "@P_OldTestPerfID", GetValue(old.TestPerfID));

                return db.ExecuteNonQuery(cmd);
            }
        }

        public override int Delete(Database db, string where)
        {
            string sql = "delete from Sales where " + where;
            using (DbCommand cmd = db.NewCommand(sql))
                return db.ExecuteNonQuery(cmd);
        }

        public override int DeleteByPk(Database db, object instance)
        {
            TesteGen.Poco.Sales value = (TesteGen.Poco.Sales)instance;
            string sql = "DELETE FROM Sales where TestPerfID = @P_TestPerfID";
            using (DbCommand cmd = db.NewCommand(sql))
            {
                db.AddWithValue(cmd, "@P_TestPerfID", GetValue(value.TestPerfID));

                return cmd.ExecuteNonQuery();
            }
        }

        public override int Delete(Database db, string where, params Parameter[] parameters)
        {
            string sql = "delete from Sales where " + where;
            using (DbCommand cmd = db.NewCommand(sql))
                return db.ExecuteNonQuery(cmd, parameters);
        }

        public override int Delete(Database db, string where, int commandTimeout, params Parameter[] parameters)
        {
            string sql = "delete from Sales where " + where;
            using (DbCommand cmd = db.NewCommand(sql, commandTimeout))
                return db.ExecuteNonQuery(cmd, parameters);
        }

        public override int Save(Database db, object instance)
        {
            //TesteGen.Poco.Sales value = (TesteGen.Poco.Sales)instance;
            //if (value.RecordStatus == RecordStatus.Existing)
            //    return update(db, value);
            //else if (value.RecordStatus == RecordStatus.New)
            //    return insert(db, value);
            //else // if (value.RecordStatus == RecordStatus.Deleted)
            //    return Delete(db, value);
            throw new Exception("To use the Save method, the class must inherit from \"Record\". Use INSERT, UPDATE, and DELETE directly");
        }

        public override int Save(Database db, object instance, EnumSaveMode saveMode)
        {
            //TesteGen.Poco.Sales value = (TesteGen.Poco.Sales)instance;
            //if (value.RecordStatus == RecordStatus.Existing)
            //    return Update(db, value, saveMode);
            //else if (value.RecordStatus == RecordStatus.New)
            //    return Insert(db, value, saveMode);
            //else // if (value.RecordStatus == RecordStatus.Deleted)
            //    return Delete(db, value);
            throw new Exception("To use the Save method, the class must inherit from \"Record\". Use INSERT, UPDATE, and DELETE directly");
        }

        public override void SaveList(Database db, object instance, EnumSaveMode saveMode, bool continueOnError)
        {
            List<TesteGen.Poco.Sales> values = (List<TesteGen.Poco.Sales>)instance;
            bool isTran = db.UsingTransaction;

            if (!isTran)
                db.BeginTransaction();

            foreach (TesteGen.Poco.Sales value in values)
            {
                try
                {
                    Save(db, value, saveMode);
                }
                catch (Exception ex)
                {
                    if (!continueOnError)
                    {
                        db.Rollback();
                        throw ex;
                    }
                }
            }

            if (!isTran)
                db.Commit();
        }

        public override int DeleteAll(Database db, object[] instance)
        {
            return 1;
        }

        public override object Select(Database db)
        {
            return select(db, false);
        }

        public override object Select(Database db, bool concurrency)
        {
            return select(db, concurrency);
        }

        public override object Select(Database db, string where)
        {
            return select(db, where, false);
        }

        public override object Select(Database db, string where, bool concurrency)
        {
            return select(db, where, concurrency);
        }

        public override object Select(Database db, string where, params Parameter[] parameters)
        {
            return select(db, where, false, parameters);
        }

        public override object Select(Database db, object _filter, EnumDbFilter mode = EnumDbFilter.AndLike, bool concurrency = false, int commandTimeout = 30)
        {
            TesteGen.Poco.Sales filter = (TesteGen.Poco.Sales)_filter;
            string sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales {0} {1} {2}";

            StringBuilder where = new StringBuilder();
            string op = (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.AndEqual) ? " AND " : " OR ";

            List<TesteGen.Poco.Sales> list = new List<TesteGen.Poco.Sales>();

            using (var cmd = db.NewCommand("", commandTimeout, CommandType.Text))
            {
                int index = 0;
                if (filter.TestPerfID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_TestPerfID", filter.TestPerfID);
                    where.AppendFormat("TestPerfID = {0} ", "@P_TestPerfID");
                }
                if (!string.IsNullOrEmpty(filter.CarrierTrackingNumber))
                {
                    if (++index > 1)
                        where.Append(op);

                    if (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.OrLike)
                    {
                        db.AddWithValue(cmd, "@P_CarrierTrackingNumber", "%" + filter.CarrierTrackingNumber + "%");
                        where.AppendFormat("CarrierTrackingNumber LIKE {0} ", "@P_CarrierTrackingNumber");
                    }
                    else
                    {
                        db.AddWithValue(cmd, "@P_CarrierTrackingNumber", filter.CarrierTrackingNumber);
                        where.AppendFormat("CarrierTrackingNumber = {0} ", "@P_CarrierTrackingNumber");
                    }
                }
                if (filter.OrderQty != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_OrderQty", filter.OrderQty);
                    where.AppendFormat("OrderQty = {0} ", "@P_OrderQty");
                }
                if (filter.ProductID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_ProductID", filter.ProductID);
                    where.AppendFormat("ProductID = {0} ", "@P_ProductID");
                }
                if (filter.SpecialOfferID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_SpecialOfferID", filter.SpecialOfferID);
                    where.AppendFormat("SpecialOfferID = {0} ", "@P_SpecialOfferID");
                }
                if (filter.UnitPrice != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_UnitPrice", filter.UnitPrice);
                    where.AppendFormat("UnitPrice = {0} ", "@P_UnitPrice");
                }
                if (filter.UnitPriceDiscount != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_UnitPriceDiscount", filter.UnitPriceDiscount);
                    where.AppendFormat("UnitPriceDiscount = {0} ", "@P_UnitPriceDiscount");
                }
                if (filter.LineTotal != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_LineTotal", filter.LineTotal);
                    where.AppendFormat("LineTotal = {0} ", "@P_LineTotal");
                }
                if (!string.IsNullOrEmpty(filter.LongText))
                {
                    if (++index > 1)
                        where.Append(op);

                    if (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.OrLike)
                    {
                        db.AddWithValue(cmd, "@P_LongText", "%" + filter.LongText + "%");
                        where.AppendFormat("LongText LIKE {0} ", "@P_LongText");
                    }
                    else
                    {
                        db.AddWithValue(cmd, "@P_LongText", filter.LongText);
                        where.AppendFormat("LongText = {0} ", "@P_LongText");
                    }
                }
                if (filter.rowguid != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_rowguid", filter.rowguid);
                    where.AppendFormat("rowguid = {0} ", "@P_rowguid");
                }
                if (filter.ModifiedDate != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_ModifiedDate", filter.ModifiedDate);
                    where.AppendFormat("ModifiedDate = {0} ", "@P_ModifiedDate");
                }

                cmd.CommandText = string.Format(sql, (where.Length > 0 ? " WHERE " + where.ToString() : ""), "", "");

                using (DbDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        TesteGen.Poco.Sales value = new TesteGen.Poco.Sales();
                        readRecord(dr, ref value);
                        //if (concurrency)
                        //    value.RecordOriginal = value.CloneT();
                        list.Add(value);
                    }
                }
            }
            return list;
        }

        public override long Count(Database db, object _filter, EnumDbFilter mode = EnumDbFilter.AndLike, int commandTimeout = 30)
        {
            TesteGen.Poco.Sales filter = (TesteGen.Poco.Sales)_filter;
            string sql = "select count(*) from Sales {0} ";

            StringBuilder where = new StringBuilder();
            string op = (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.AndEqual) ? " AND " : " OR ";

            List<TesteGen.Poco.Sales> list = new List<TesteGen.Poco.Sales>();

            using (var cmd = db.NewCommand("", commandTimeout, CommandType.Text))
            {
                int index = 0;

                if (filter.TestPerfID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_TestPerfID", filter.TestPerfID);
                    where.AppendFormat("TestPerfID = {0} ", "@P_TestPerfID");
                }
                if (!string.IsNullOrEmpty(filter.CarrierTrackingNumber))
                {
                    if (++index > 1)
                        where.Append(op);

                    if (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.OrLike)
                    {
                        db.AddWithValue(cmd, "@P_CarrierTrackingNumber", "%" + filter.CarrierTrackingNumber + "%");
                        where.AppendFormat("CarrierTrackingNumber LIKE {0} ", "@P_CarrierTrackingNumber");
                    }
                    else
                    {
                        db.AddWithValue(cmd, "@P_CarrierTrackingNumber", filter.CarrierTrackingNumber);
                        where.AppendFormat("CarrierTrackingNumber = {0} ", "@P_CarrierTrackingNumber");
                    }
                }
                if (filter.OrderQty != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_OrderQty", filter.OrderQty);
                    where.AppendFormat("OrderQty = {0} ", "@P_OrderQty");
                }
                if (filter.ProductID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_ProductID", filter.ProductID);
                    where.AppendFormat("ProductID = {0} ", "@P_ProductID");
                }
                if (filter.SpecialOfferID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_SpecialOfferID", filter.SpecialOfferID);
                    where.AppendFormat("SpecialOfferID = {0} ", "@P_SpecialOfferID");
                }
                if (filter.UnitPrice != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_UnitPrice", filter.UnitPrice);
                    where.AppendFormat("UnitPrice = {0} ", "@P_UnitPrice");
                }
                if (filter.UnitPriceDiscount != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_UnitPriceDiscount", filter.UnitPriceDiscount);
                    where.AppendFormat("UnitPriceDiscount = {0} ", "@P_UnitPriceDiscount");
                }
                if (filter.LineTotal != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_LineTotal", filter.LineTotal);
                    where.AppendFormat("LineTotal = {0} ", "@P_LineTotal");
                }
                if (!string.IsNullOrEmpty(filter.LongText))
                {
                    if (++index > 1)
                        where.Append(op);

                    if (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.OrLike)
                    {
                        db.AddWithValue(cmd, "@P_LongText", "%" + filter.LongText + "%");
                        where.AppendFormat("LongText LIKE {0} ", "@P_LongText");
                    }
                    else
                    {
                        db.AddWithValue(cmd, "@P_LongText", filter.LongText);
                        where.AppendFormat("LongText = {0} ", "@P_LongText");
                    }
                }
                if (filter.rowguid != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_rowguid", filter.rowguid);
                    where.AppendFormat("rowguid = {0} ", "@P_rowguid");
                }
                if (filter.ModifiedDate != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_ModifiedDate", filter.ModifiedDate);
                    where.AppendFormat("ModifiedDate = {0} ", "@P_ModifiedDate");
                }

                cmd.CommandText = string.Format(sql, (where.Length > 0 ? " WHERE " + where.ToString() : ""), "", "");

                return Convert.ToInt64(cmd.ExecuteScalar());
            }
        }

        //public Record CreateInstance()
        //{
        //    throw new NotImplementedException();
        //}

        public override object SelectPage(Database db, int start, int pageSize = 20, string sort = null)
        {
            return selectPage(db, null, start, pageSize, sort, false, null);
        }

        public override object SelectPage(Database db, int start, int pageSize = 20, string sort = null, bool concurrency = false)
        {
            return selectPage(db, null, start, pageSize, sort, concurrency, null);
        }

        public override object SelectPage(Database db, string where, int start, int pageSize = 20, string sort = null)
        {
            return selectPage(db, where, start, pageSize, sort, false, null);
        }

        public override object SelectPage(Database db, string where, int start, int pageSize = 20, string sort = null, bool concurrency = false)
        {
            return selectPage(db, where, start, pageSize, sort, concurrency, null);
        }

        public override object SelectPage(Database db, string where, int start, int pageSize = 20, string sort = null, params Parameter[] parameters)
        {
            return selectPage(db, where, start, pageSize, sort, false, parameters);
        }

        public override object SelectPage(Database db, string where, int start, int pageSize = 20, string sort = null, bool concurrency = false, params Parameter[] parameters)
        {
            return selectPage(db, where, start, pageSize, sort, concurrency, parameters);
        }

        private object selectPage(Database db, string where, int start, int pageSize, string sort, bool concurrency, params Parameter[] parameters)
        {
            sort = sort ?? "TestPerfID";
            string sql =
    @"
WITH Ordered AS
(
    SELECT TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate,
    ROW_NUMBER() OVER (ORDER BY {2}) AS Row____Number
    FROM Sales {3}
) 
SELECT TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate  
FROM Ordered
WHERE Row____Number BETWEEN {0} AND {1};
";
            sql = string.Format(sql, (start + 1), (start + pageSize), sort, (where ?? ""));
            return readReader(db, sql, concurrency, parameters);
        }

        public override object SelectPage(Database db, object _filter, int start, int pageSize = 20, string sort = null, EnumDbFilter mode = EnumDbFilter.AndLike, int commandTimeout = 30, bool concurrency = false, params Parameter[] parameters)
        {
            TesteGen.Poco.Sales filter = (TesteGen.Poco.Sales)_filter;
            sort = sort ?? "TestPerfID";
            StringBuilder where = new StringBuilder();
            string op = (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.AndEqual) ? " AND " : " OR ";

            List<TesteGen.Poco.Sales> list = new List<TesteGen.Poco.Sales>();

            using (var cmd = db.NewCommand("", commandTimeout, CommandType.Text))
            {
                int index = 0;
                if (filter.TestPerfID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_TestPerfID", filter.TestPerfID);
                    where.AppendFormat("TestPerfID = {0} ", "@P_TestPerfID");
                }
                if (!string.IsNullOrEmpty(filter.CarrierTrackingNumber))
                {
                    if (++index > 1)
                        where.Append(op);

                    if (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.OrLike)
                    {
                        db.AddWithValue(cmd, "@P_CarrierTrackingNumber", "%" + filter.CarrierTrackingNumber + "%");
                        where.AppendFormat("CarrierTrackingNumber LIKE {0} ", "@P_CarrierTrackingNumber");
                    }
                    else
                    {
                        db.AddWithValue(cmd, "@P_CarrierTrackingNumber", filter.CarrierTrackingNumber);
                        where.AppendFormat("CarrierTrackingNumber = {0} ", "@P_CarrierTrackingNumber");
                    }
                }
                if (filter.OrderQty != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_OrderQty", filter.OrderQty);
                    where.AppendFormat("OrderQty = {0} ", "@P_OrderQty");
                }
                if (filter.ProductID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_ProductID", filter.ProductID);
                    where.AppendFormat("ProductID = {0} ", "@P_ProductID");
                }
                if (filter.SpecialOfferID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_SpecialOfferID", filter.SpecialOfferID);
                    where.AppendFormat("SpecialOfferID = {0} ", "@P_SpecialOfferID");
                }
                if (filter.UnitPrice != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_UnitPrice", filter.UnitPrice);
                    where.AppendFormat("UnitPrice = {0} ", "@P_UnitPrice");
                }
                if (filter.UnitPriceDiscount != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_UnitPriceDiscount", filter.UnitPriceDiscount);
                    where.AppendFormat("UnitPriceDiscount = {0} ", "@P_UnitPriceDiscount");
                }
                if (filter.LineTotal != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_LineTotal", filter.LineTotal);
                    where.AppendFormat("LineTotal = {0} ", "@P_LineTotal");
                }
                if (!string.IsNullOrEmpty(filter.LongText))
                {
                    if (++index > 1)
                        where.Append(op);

                    if (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.OrLike)
                    {
                        db.AddWithValue(cmd, "@P_LongText", "%" + filter.LongText + "%");
                        where.AppendFormat("LongText LIKE {0} ", "@P_LongText");
                    }
                    else
                    {
                        db.AddWithValue(cmd, "@P_LongText", filter.LongText);
                        where.AppendFormat("LongText = {0} ", "@P_LongText");
                    }
                }
                if (filter.rowguid != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_rowguid", filter.rowguid);
                    where.AppendFormat("rowguid = {0} ", "@P_rowguid");
                }
                if (filter.ModifiedDate != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_ModifiedDate", filter.ModifiedDate);
                    where.AppendFormat("ModifiedDate = {0} ", "@P_ModifiedDate");
                }

                string w = where.ToString();
                string sql =
        @"
WITH Ordered AS
(
    SELECT TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate,
    ROW_NUMBER() OVER (ORDER BY {2}) AS Row____Number
    FROM Sales {3}
) 
SELECT TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate  
FROM Ordered
WHERE Row____Number BETWEEN {0} AND {1};
";
                sql = string.Format(sql, (start + 1), (start + pageSize), sort, (where.Length > 0 ? " WHERE " + w : ""));

                cmd.CommandText = sql;

                using (DbDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        TesteGen.Poco.Sales value = new TesteGen.Poco.Sales();
                        readRecord(dr, ref value);
                        //if (concurrency)
                        //    value.RecordOriginal = value.CloneT();
                        list.Add(value);
                    }
                }
            }
            return list;
        }

        public override object SelectTop(Database db, int top)
        {
            return selectTop(db, top, false);
        }

        public override object SelectTop(Database db, int top, bool concurrency)
        {
            return selectTop(db, top, concurrency);
        }

        public override object SelectTop(Database db, int top, string where)
        {
            return selectTop(db, top, where, false);
        }

        public override object SelectTop(Database db, int top, string where, bool concurrency)
        {
            return selectTop(db, top, where, concurrency);
        }

        public override long Count(Database db)
        {
            return count(db, null);
        }

        public override long Count(Database db, string where)
        {
            return count(db, where);
        }

        public override long Max(Database db, string columnName)
        {
            return max(db, columnName, null);
        }

        public override long Max(Database db, string columnName, string where)
        {
            return max(db, columnName, where);
        }

        public override object SelectColumns(Database db, bool concurrency, params string[] columns)
        {
            return selectColumns(db, concurrency, columns);
        }

        public override object SelectColumns(Database db, string where, bool concurrency, params string[] columns)
        {
            return selectColumns(db, where, concurrency, columns);
        }

        public override object SelectSingle(Database db)
        {
            return selectSingle(db, false);
        }

        public override object SelectSingle(Database db, bool concurrency)
        {
            return selectSingle(db, concurrency);
        }

        public override object SelectSingle(Database db, string where)
        {
            return selectSingle(db, where, false);
        }

        public override object SelectSingle(Database db, string where, bool concurrency)
        {
            return selectSingle(db, where, concurrency);
        }

        public override object SelectSingle(Database db, string where, params Parameter[] parameters)
        {
            string sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales where " + where;
            return readReaderSingle(db, sql, parameters);
        }

        public override object SelectSingle(Database db, object _filter, EnumDbFilter mode = EnumDbFilter.AndLike, bool concurrency = false, int commandTimeout = 30)
        {
            TesteGen.Poco.Sales filter = (TesteGen.Poco.Sales)_filter;
            string sql = "select TestPerfID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate from Sales {0} {1} {2}";

            StringBuilder where = new StringBuilder();
            string op = (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.AndEqual) ? " AND " : " OR ";

            TesteGen.Poco.Sales ret = null;

            using (var cmd = db.NewCommand("", commandTimeout, CommandType.Text))
            {
                int index = 0;
                if (filter.TestPerfID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_TestPerfID", filter.TestPerfID);
                    where.AppendFormat("TestPerfID = {0} ", "@P_TestPerfID");
                }
                if (!string.IsNullOrEmpty(filter.CarrierTrackingNumber))
                {
                    if (++index > 1)
                        where.Append(op);

                    if (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.OrLike)
                    {
                        db.AddWithValue(cmd, "@P_CarrierTrackingNumber", "%" + filter.CarrierTrackingNumber + "%");
                        where.AppendFormat("CarrierTrackingNumber LIKE {0} ", "@P_CarrierTrackingNumber");
                    }
                    else
                    {
                        db.AddWithValue(cmd, "@P_CarrierTrackingNumber", filter.CarrierTrackingNumber);
                        where.AppendFormat("CarrierTrackingNumber = {0} ", "@P_CarrierTrackingNumber");
                    }
                }
                if (filter.OrderQty != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_OrderQty", filter.OrderQty);
                    where.AppendFormat("OrderQty = {0} ", "@P_OrderQty");
                }
                if (filter.ProductID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_ProductID", filter.ProductID);
                    where.AppendFormat("ProductID = {0} ", "@P_ProductID");
                }
                if (filter.SpecialOfferID != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_SpecialOfferID", filter.SpecialOfferID);
                    where.AppendFormat("SpecialOfferID = {0} ", "@P_SpecialOfferID");
                }
                if (filter.UnitPrice != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_UnitPrice", filter.UnitPrice);
                    where.AppendFormat("UnitPrice = {0} ", "@P_UnitPrice");
                }
                if (filter.UnitPriceDiscount != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_UnitPriceDiscount", filter.UnitPriceDiscount);
                    where.AppendFormat("UnitPriceDiscount = {0} ", "@P_UnitPriceDiscount");
                }
                if (filter.LineTotal != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_LineTotal", filter.LineTotal);
                    where.AppendFormat("LineTotal = {0} ", "@P_LineTotal");
                }
                if (!string.IsNullOrEmpty(filter.LongText))
                {
                    if (++index > 1)
                        where.Append(op);

                    if (mode == EnumDbFilter.AndLike || mode == EnumDbFilter.OrLike)
                    {
                        db.AddWithValue(cmd, "@P_LongText", "%" + filter.LongText + "%");
                        where.AppendFormat("LongText LIKE {0} ", "@P_LongText");
                    }
                    else
                    {
                        db.AddWithValue(cmd, "@P_LongText", filter.LongText);
                        where.AppendFormat("LongText = {0} ", "@P_LongText");
                    }
                }
                if (filter.rowguid != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_rowguid", filter.rowguid);
                    where.AppendFormat("rowguid = {0} ", "@P_rowguid");
                }
                if (filter.ModifiedDate != null)
                {
                    if (++index > 1)
                        where.Append(op);
                    db.AddWithValue(cmd, "@P_ModifiedDate", filter.ModifiedDate);
                    where.AppendFormat("ModifiedDate = {0} ", "@P_ModifiedDate");
                }

                sql = string.Format(sql, (where.Length > 0 ? " WHERE " + where.ToString() : ""), "", "");
                sql = db.Provider.SetTop(sql, 1);
                cmd.CommandText = sql;

                using (DbDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        ret = new TesteGen.Poco.Sales();
                        readRecord(dr, ref ret);
                        //if (concurrency)
                        //    ret.RecordOriginal = ret.CloneT();
                    }
                }
            }
            return ret;
        }

        public override object SelectArray(Database db)
        {
            return select(db, false);
        }

        public override object SelectArray(Database db, bool concurrency)
        {
            return select(db, concurrency).ToArray();
        }

        public override object SelectArray(Database db, string where)
        {
            return select(db, where, false);
        }

        public override object SelectArray(Database db, string where, bool concurrency)
        {
            return select(db, where, concurrency).ToArray();
        }

        public override string SelectToJson(Database db)
        {
            return SelectToJson(db, false);
        }

        public override string SelectToJson(Database db, bool concurrency)
        {
            return SerializeToJson<TesteGen.Poco.Sales>(select(db, concurrency));
        }

        public override string SelectToJson(Database db, string where)
        {
            return SelectToJson(db, where, false);
        }

        public override string SelectToJson(Database db, string where, bool concurrency)
        {
            return SerializeToJson<TesteGen.Poco.Sales>(select(db, where, concurrency));
        }

        public override object Query(Database db, string sql)
        {
            return readReaderSql(db, sql, false);
        }

        public override object Query(Database db, string sql, bool concurrency)
        {
            return readReaderSql(db, sql, concurrency);
        }

        public override object SelectByPk(Database db, object instance)
        {
            return selectByPk(db, instance, true);
        }

        public override object SelectByPk(Database db, object instance, bool concurrency)
        {
            return selectByPk(db, instance, concurrency);
        }

        public override string UpdateFromJson(Database db, string json, EnumSaveMode saveMode)
        {
            TesteGen.Poco.Sales rec = DeserializeFromJson<TesteGen.Poco.Sales>(json);
            Update(db, rec, saveMode);
            return SerializeToJson<TesteGen.Poco.Sales>(rec);
        }

        public override string SaveFromJson(Database db, string json, EnumSaveMode saveMode)
        {
            TesteGen.Poco.Sales rec = DeserializeFromJson<TesteGen.Poco.Sales>(json);
            Save(db, rec, saveMode);
            return SerializeToJson<TesteGen.Poco.Sales>(rec);
        }

        public override string SaveListFromJson(Database db, string json, EnumSaveMode saveMode, bool continueOnError)
        {
            List<TesteGen.Poco.Sales> rec = DeserializeFromJson<List<TesteGen.Poco.Sales>>(json);
            SaveList(db, rec, saveMode, continueOnError);
            return SerializeToJson<List<TesteGen.Poco.Sales>>(rec);
        }

        public override int Insert(Database db, object instance)
        {
            return insert(db, instance);
        }

        public override int Insert(Database db, object instance, EnumSaveMode saveMode, bool concurrency = false)
        {
            if (saveMode == EnumSaveMode.None)
                return insert(db, instance);
            else
                return insertRequery(db, instance, concurrency);
        }

        public static string Concat(string endSeparator, params object[] values)
        {
            return Concat(null, endSeparator, values);
        }

        public static string Concat(string beforeSeparator, string endSeparator, params object[] values)
        {
            System.Text.StringBuilder text = new System.Text.StringBuilder();
            for (int i = 0; i < values.Length; i++)
            {
                if (beforeSeparator != null)
                    text.Append(beforeSeparator);
                text.Append(values[i].ToString());
                if (i < values.Length - 1 && endSeparator != null)
                    text.Append(endSeparator);
            }
            return text.ToString();
        }

        public override int InsertXml(Database db, object instance, EnumSaveMode saveMode = EnumSaveMode.None)
        {

            List<TesteGen.Poco.Sales> values = (List<TesteGen.Poco.Sales>)instance;

            // create DataTable
            DataTable tb = new DataTable("Table");
            tb.Columns.Add("CarrierTrackingNumber", typeof(String));
            tb.Columns.Add("OrderQty", typeof(Int32));
            tb.Columns.Add("ProductID", typeof(Int32));
            tb.Columns.Add("SpecialOfferID", typeof(Int32));
            tb.Columns.Add("UnitPrice", typeof(Decimal));
            tb.Columns.Add("UnitPriceDiscount", typeof(Decimal));
            tb.Columns.Add("LineTotal", typeof(Decimal));
            tb.Columns.Add("LongText", typeof(String));
            tb.Columns.Add("rowguid", typeof(Guid));
            tb.Columns.Add("ModifiedDate", typeof(DateTime));
            foreach (var value in values)
            {
                DataRow row = tb.NewRow();
                row["CarrierTrackingNumber"] = value.CarrierTrackingNumber;
                row["OrderQty"] = value.OrderQty;
                row["ProductID"] = value.ProductID;
                row["SpecialOfferID"] = value.SpecialOfferID;
                row["UnitPrice"] = value.UnitPrice;
                row["UnitPriceDiscount"] = value.UnitPriceDiscount;
                row["LineTotal"] = value.LineTotal;
                row["LongText"] = value.LongText;
                row["rowguid"] = value.rowguid;
                row["ModifiedDate"] = value.ModifiedDate;
                tb.Rows.Add(row);
            }

            string sql =
            @"
declare @xmlData xml = @Xml
BEGIN
    DECLARE @idInt int;

    EXEC sp_xml_preparedocument @idInt OUTPUT, @xmlData;

    INSERT INTO Sales(CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate)
    SELECT CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal, LongText, rowguid, ModifiedDate
    FROM OPENXML (@idInt, '/DocumentElement/*')
    WITH 
    (
        CarrierTrackingNumber nvarchar(50) 'CarrierTrackingNumber',
OrderQty int 'OrderQty',
ProductID int 'ProductID',
SpecialOfferID int 'SpecialOfferID',
UnitPrice money 'UnitPrice',
UnitPriceDiscount money 'UnitPriceDiscount',
LineTotal numeric 'LineTotal',
LongText varchar(1000) 'LongText',
rowguid uniqueidentifier 'rowguid',
ModifiedDate datetime 'ModifiedDate'
    )

    select CAST( SCOPE_IDENTITY() - @@ROWCOUNT + 1 AS INT);
    exec sp_xml_removedocument @idInt;
END;";

            using (System.IO.StringWriter swStringWriter = new System.IO.StringWriter())
            {
                // Datatable as XML format 
                tb.WriteXml(swStringWriter);
                // Datatable as XML string 
                string xml = swStringWriter.ToString();
                using (var cmd = db.NewCommand(sql, 0))
                {
                    db.AddParameter(cmd, "@xml", DbType.Xml, xml, ParameterDirection.Input);
                    int firstId = Convert.ToInt32(cmd.ExecuteScalar());
                    return firstId;
                }
            }

        }

        public void CheckConcurrency(Database db, TesteGen.Poco.Sales value)
        {
            //var original = value.RecordOriginal as TesteGen.Poco.Sales;
            //if (original == null)
            //    return;
            //var current = (TesteGen.Poco.Sales)SelectByPk(db, original);
            //if (current == null)
            //    throw new Exception("Original record has been deleted");
            //else if (!original.IsEqual(current))
            //    throw new DBConcurrencyException();
        }

        public override DataClass CloneT()
        {
            return new DataClassSales();
        }

    }

}